<template>
  <div ref="chart" style="height: 100%" />
</template>
<script>
import { Chart } from '@antv/g2';
import { getTicks } from '@/utils/chartScale.js';
export default {
  name: 'BarChart2',
  props: {
    data: { type: Array, required: true },
    x: { type: String, default: 'x' },
    y: { type: String, default: 'y' },
    s: { type: String, default: 's' },
    color: { type: Array, required: true },
    unit: { type: String, required: true },
    text: { type: String, required: true },
    barWidth: { type: Number, default: 20 }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    data() {
      this.updateData();
    },
    unit() {
      this.chart.annotation().clear(true);
      this.addAnnotation();
    }
  },
  mounted() {
    this.initChart();
    this.drawChart();
    this.addAnnotation();
  },
  methods: {
    getTicks,
    initChart() {
      const ref = this.$refs.chart;
      const ratio = window.ratio || 1;
      this.chart = new Chart({
        container: ref,
        width: ref.clientWidth,
        height: ref.clientHeight,
        appendPadding: [22 * ratio, 0, 2 * ratio, 0]
      });
    },
    drawChart() {
      const ratio = window.ratio || 1;
      const x = this.x;
      const y = this.y;
      const s = this.s;
      this.chart.data(this.data);
      this.chart.scale(y, {
        min: 0,
        tickCount: 5,
        tickMethod: this.getTicks
      });
      this.chart.tooltip({
        showMarkers: false,
        customItems: (items) => {
          items.forEach((item, idx) => {
            item.name = this.text[idx];
          });
          return items;
        }
        // domStyles: {
        //   'g2-tooltip-title': {
        //     fontSize: '.875rem',
        //     color: '#8D8D8D'
        //   },
        //   'g2-tooltip-list-item': {
        //     fontSize: 0,
        //     overflow: 'hidden'
        //   },
        //   'g2-tooltip-name': {
        //     fontSize: '.75rem',
        //     color: '#8D8D8D'
        //   },
        //   'g2-tooltip-value': {
        //     fontSize: '.75rem',
        //     color: 'rgba(0, 0, 0, 0.6)',
        //     fontWeight: '780'
        //   }
        // }
      });
      // x轴
      this.chart.axis(x, {
        label: {
          style: {
            fill: '#ADADAD',
            fontSize: 12 * ratio,
            fontFamily: 'Helvetica-regular'
          },
          offset: 5 * ratio
        },
        line: {
          style: {
            stroke: '#FFFFFF',
            strokeOpacity: 0.4,
            lineWidth: 1 * ratio
          }
        },
        tickLine: null
      });
      // y轴
      this.chart.axis(y, {
        label: {
          style: {
            fill: '#ADADAD',
            fontSize: 12 * ratio,
            fontFamily: 'Helvetica-regular'
          },
          autoHide: false,
          offset: 5 * ratio
        },
        grid: null,
        line: null
      });
      this.chart.legend(false);
      this.chart
        .interval()
        .size(this.barWidth)
        .position(`${x}*${y}`)
        .color(s, this.color)
        .animate({
          appear: {
            animation: 'scale-in-y',
            delay: 0,
            duration: 2000
          },
          update: {
            animation: 'scale-in-y',
            delay: 0,
            duration: 2000
          }
        });
      // this.chart.interaction('active-region');
      this.chart.render();
    },
    addAnnotation() {
      const ref = this.$refs.chart;
      const ratio = window.ratio || 1;
      // 单位标注
      this.chart.annotation().text({
        content: this.unit,
        position: ['end', 'start'],
        style: {
          fill: '#ADADAD',
          fontSize: 12 * ratio,
          fontFamily: 'Helvetica-regular'
        },
        background: {
          padding: 2 * ratio
        },
        offsetY: -ref.clientHeight + 28 * ratio,
        offsetX: -ref.clientWidth + this.chart.autoPadding.right
      });
      this.chart.render();
    },
    // 数据更新
    updateData() {
      this.chart.changeData(this.data);
    }
  }
};
</script>
